package com.ant.backstage.controller.common;

import com.ant.backstage.facade.ApiReturn;
import com.ant.backstage.facade.request.CheckVerifyCodeRequest;
import com.ant.backstage.facade.request.GetVerificationCodeRequest;
import com.ant.backstage.facade.request.MailRequest;
import com.ant.backstage.service.VerificationICodeService;
import com.ant.backstage.utils.IpAddrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author zhangyu
 * @Description
 * @Date 15:23 2021/12/28
 * @Param 公共接口 验证码 ，短信，邮件验证， 消息推送 等
 * @return
 **/

@RestController
@RequestMapping("/api/v1/common")
@Api(tags = "公共接口")
public class VerificationController {

    private static Logger logger = LogManager.getLogger(VerificationController.class);

    @Resource
    VerificationICodeService verificationICodeService;

/*
    出现循环调用的问题：


    异常：
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'verificationController':
    Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'verificationService': Injection of resource dependencies failed;
    nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
    Bean named 'verificationService' is expected to be of type 'com.ant.backstage.service.VerificationICodeService' but was actually of type 'com.ant.backstage.biz.serviceImpl.VerficationFacadeServiceImpl'
*/


    @ApiOperation(value = "找回密码获取验证码", response = ApiReturn.class)
    @RequestMapping(value = "/getVerificationCode", method = RequestMethod.POST)
    public ApiReturn getVerificationCode(HttpServletRequest request, @RequestBody GetVerificationCodeRequest getRequest) {
        String ipAddr = IpAddrUtil.getIpAddress(request);
        getRequest.setCurrentIp(ipAddr);
        ApiReturn apiReturn = verificationICodeService.getVerificationCode(getRequest);
        return apiReturn;
    }

    @ApiOperation(value = "校验验证码", response = ApiReturn.class)
    @RequestMapping(value = "/checkVerifyCode", method = RequestMethod.POST)
    public ApiReturn checkVerificationCode(HttpServletRequest request, @RequestBody CheckVerifyCodeRequest checkVerifyCodeRequest,
                                           HttpServletResponse response) {
        String ipAddr = IpAddrUtil.getIpAddress(request);
        checkVerifyCodeRequest.setCurrentIp(ipAddr);
        ApiReturn apiReturn = verificationICodeService.checkVerificationCode(checkVerifyCodeRequest, response);
        return apiReturn;
    }

    @ApiOperation(value = "发送邮件", response = ApiReturn.class)
    @RequestMapping(value = "/sendMail", method = RequestMethod.POST)
    public ApiReturn sendMail(HttpServletRequest request, @RequestBody MailRequest mailRequest) {
        String ipAddr = IpAddrUtil.getIpAddress(request);
        mailRequest.setCurrentIp(ipAddr);
        ApiReturn apiReturn = verificationICodeService.sendMail(mailRequest);
        return apiReturn;
    }
}
